Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    cameraTotal: '',
    cameraList: [],
    imgList: [],
    iconAdd: '../../images/加号.png',
    // 播放图标
    bg: false,
  },
  jumpHome: function () {
    wx.redirectTo({
      url: '../home/home',
    })
  },
  jumpWarn: function () {
    wx.redirectTo({
      url: '../warn/warn',
    })
  },
  jumpCamera: function () {
    wx.redirectTo({
      url: 'camera',
    })
  },
  jumpServer: function () {
    wx.redirectTo({
      url: '../server/server',
    })
  },
  cameraManager: function () {
    wx.navigateTo({
      url: '/pages/cameraManage/cameraManage',
    })
  },

  // 跳转摄像头页面
  addCamera() {
    wx.navigateTo({
      url: '../addCamera/addCamera',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        })

        let header = {
          'context-type': 'application/json',
          'UserToken': that.data.token,
          'Cookie': wx.getStorageSync("sessionid")
        }
        wx.request({
          url: 'https://www.2edge.cn/rest/camera/queryList',
          method: 'post',
          header: header,
          data: {
            "pageNum": '',
            "pageSise": '',
            "conditions": []
          },
          success: function (res) {
            let recordArr = res.data.data.records
            
            let imgList = []
            
            recordArr.forEach(function (item, index) {
              
              imgList[index] = { "wss": false, "imageUrl": "https://www.2edge.cn/cameras/" + item.id + ".jpg" }
            });
            that.setData({
              imgList: imgList
            })
            that.setData({
              cameraTotal: res.data.data.total,
              cameraList: res.data.data.records
            })
          }
        })
      }
    })
  },


  // 播放视频
  startVedio: function (e) {
    // console.log(e);
    let that = this;
    console.log(e.currentTarget.dataset.index)
    // console.log(e.currentTarget.dataset.deviceid)
    var videoWs = that.data.imgList[e.currentTarget.dataset.index].wss
    var ListProp = "imgList[" + e.currentTarget.dataset.index + "].imageUrl"
    var ListPropWs = "imgList[" + e.currentTarget.dataset.index + "].wss"

    if (!videoWs) {
      wx.connectSocket({
        url: "wss://www.2edge.cn/aiclient/video/read/" + e.currentTarget.dataset.deviceid + "/" + e.currentTarget.dataset.id,
        success: function (e) {
          console.log('回调成功')
        },
        fail: function (e) {
          console.log('获取失败');
          console.log(e);

        }
      })
      wx.onSocketOpen(function (res) {
        console.log("链接成功!")
        that.setData({
          [ListPropWs]: true
        })
      })
      wx.onSocketMessage(function (data) {
        // console.log(data)
        that.setData({
          [ListProp]: "data:image/jpeg;base64," + data.data
        })
      })
    } else {
      wx.closeSocket()
      that.setData({
        [ListPropWs]: false
      })
    }
  },
  //修改摄像头信息
  updateCamera: function(e){
    // console.log(e);
    // 当前摄像头ID
    let index = e.currentTarget.dataset.index

    wx.navigateTo({
      url: '../cameraManage/cameraManage?index=' + index ,
    })
    
  }
})
